<?php
/**
 * 前台影集管理模块类
 * @author phills.li
 *
 */
class AlbumAction extends Action {	
    public function index() {        
        $this->display();
    }
    
    
   /**
     *  ajax 创建影集
     */
    public function ajaxAlbumAdd(){
      //判断是否传入参数是否存在
      if(isset($_REQUEST['is_allow'])  &&  isset($_REQUEST['user_id']) && isset($_REQUEST['album_name']) && isset($_REQUEST['album_desc'])){
          //去掉传入参数左右2边的空格
            $is_allow     = trim($_REQUEST['is_allow']);
            $user_id      = trim($_REQUEST['user_id']);
            $album_name   = trim($_REQUEST['album_name']);
            $album_desc   = trim($_REQUEST['album_desc']);
            $album = M('MovieAlbum');
            //判断参数是否存在空值  
            if(isEmpty($is_allow,$user_id,$album_name,$album_desc)){
                    $data['is_input_movie']=$is_allow;                  
                    $data['user_id']=$user_id;
                    $data['name']=$album_name;
                    $data['description'] = $album_desc;
                    $data['create_time'] = time();
                     $id = $album->add($data);
                    if($id === FALSE){
                    $this->ajaxReturn('','添加影集失败', 0);
                    }
                    else{
                    $this->ajaxReturn('','添加影集成功', 1);
                    }
             }
             else{
              $this->ajaxReturn('','参数值不能为空', 0);
             }
         }
         else{
              $this->ajaxReturn('','请传入必须参数', 0);
         }
      }
    
    /**
      * ajax 获得用户创建的影集List 
      */
      public function ajaxGetCreateAlbum(){
            if(isset($_REQUEST['user_id'])){
                $user_id =trim($_REQUEST['user_id']);
                if(isEmpty($user_id)){
                    $album = M('MovieAlbum');
                    $map['user_id']=array('eq',$user_id);
                    $list = $album->where($map)->field('album_id,name')->select();
                    echo $album->getLastSql();
                    $this->ajaxReturn($list,'',1); 
                }else{
                $this->ajaxReturn('','参数值不能为空', 0); 
                }
            }else{
                $this->ajaxReturn('','请传入必须参数', 0);
            }
      }

     /**
       *  ajax 获取用户收藏的影集
       */
      public function  ajaxGetCollectAlbum(){
            if(isset($_REQUEST['user_id'])){
                    $user_id =trim($_REQUEST['user_id']);
                    if(isEmpty($user_id)){
                    $userInfo = M('UserInfo'); 
                    $map['user_id']=array('eq',$user_id);
                    $list =$userInfo->field('favorite_album_list')->where($map)->find();
                    $ids=  $list['favorite_album_list'];
                    //去掉开头逗号跟最后一个逗号。
                    $ids=substr($ids,1,-1);
                    unset($list);
                    $album = M('MovieAlbum');
                    $list = $album->where("album_id in ($ids)")->field('album_id,name')->select();   
                    $this->ajaxReturn($list,'',1); 
                }else{
                $this->ajaxReturn('','参数值不能为空', 0); 
                }
         }else{
            $this->ajaxReturn('','请传入必须参数', 0);
         }
      }
      
      
    /**
     *  ajax 影片关联影集
     */
    
     public function ajaxMovieToAlbum(){
         
     }
     
     /*
      *  ajax 收藏影集  
      */
     
     public function ajaxAddCollectAlbum(){
            if(isset($_REQUEST['album_id']) && isset($_REQUEST['user_id'])){
                    $album_id =trim($_REQUEST['album_id']);
                    $user_id =trim($_REQUEST['user_id']);
                    if(isEmpty($user_id,$album_id)){
                     $userInfo = M('UserInfo'); 
                     $map['user_id']=array('eq',$user_id);
                     $list =$userInfo->field('favorite_album_list')->where($map)->find();
                     $ids=  $list['favorite_album_list'];   
                             if(!isEmpty($ids)){
                                 $ids=",";
                             }
                            $ids.=($album_id.",");
                            $rows = $userInfo->where($user_id)->setField('favorite_album_list',$ids);
                            if($rows === FALSE){
                                $this->ajaxReturn('','保存失败', 0); 
                            }else{
                                $this->ajaxReturn('','收藏成功', 1);  
                            }
                    }else{
                        $this->ajaxReturn('','参数值不能为空', 0); 
                    }
            }else{
                $this->ajaxReturn('','请传入必须参数', 0);
            }
     }
 
     
     
     
}
?>